/**
 * 后台主页js
 */
var mgr= {
	hasAll:true,
	
	startEvent:function(){
		var winHeight=$(window).height();
		$('#menusUl').css('height',winHeight-54);
		$('#rightDiv').css('height',winHeight-54);
		
		//登录时间
		mgr.setLoginDate();
		setInterval("mgr.setLoginDate()",1000);
		
		typeAndFlag=typeAndFlag.substring(1,typeAndFlag.length-1);
		
		var typeAndFlags=typeAndFlag.split(',');
		
		//控制一级菜单 
		for(var i=0;i<typeAndFlags.length;i++){
			var type=typeAndFlags[i].split(';')[0];
			var flag=typeAndFlags[i].split(';')[1];
			if(type==1){
				if(flag){
					$('#menusUl ul').each(function(idx,obj){
						if($(obj).attr('id')==flag){
							$(obj).css('display','');
							$(obj).prev().css('display','');
						}
					});
				}
			}
			if(type==2){
				if(flag){
					$('#menusUl ul li').each(function(idx,obj){
						if($(obj).attr('name')==flag){
							$(obj).css('display','');
						}
					});
				}
			}
		}
		//控制右边滚动条高度情况
		$('#menusUl').children('ul').each(function(idx,el){
			if($(el).css('display')=='none'){
				mgr.hasAll=false;
			}
		});
		if(mgr.hasAll){
			$('#menusUl').css('height',winHeight);
			$('#rightDiv').css('height',winHeight);
		}
		
		//右边默认显示页面
		var firstUl=null;
		$('#menusUl').children('ul').each(function(idx,el){
			if($(el).css('display')!='none'){
				firstUl=$(el);
				return false;
			}
		});
		var firstLi=null;
		firstUl.children('li').each(function(idx,el){
			if($(el).css('display')!='none'){
				firstLi=$(el);
				$(el).addClass('cur');
				return false;
			}
		});

		if(firstLi != null){
			var firstName=firstLi.attr('name');
			var txt=firstLi.text();
			//左边生成相应的tab
			tabUtil.createTab(firstName, txt);
			//iframe加载页面
			var url=mgr.getUrl(firstName);
			$('#tabIframe').find('iframe[name="'+firstName+'"]').attr("src",url);
		}

	},
	bindEvent:function(){
		//左边菜单下拉效果
		$('#menusUl h3').click(function(){
			var ii=$($(this).find('i[name="ii"]'));
			if(ii.hasClass("fa-angle-double-up")){
				ii.removeClass("fa-angle-double-up");
				ii.addClass("fa-angle-double-down");
				$(this).next().slideUp();
			}else{
				ii.addClass("fa-angle-double-up");
				ii.removeClass("fa-angle-double-down");
				$(this).next().slideDown();
			}
			
		});
		
		$('#menusUl ul li').click(function(){
			$('#menusUl ul li').removeClass('cur');
			$(this).addClass('cur');
			
			var name=$(this).attr('name');
			var txt=$(this).text();
			//左边生成相应的tab
			tabUtil.createTab(name, txt);
			//iframe加载页面
			var url=mgr.getUrl(name);
			$('#tabIframe').find('iframe[name="'+name+'"]').attr("src",url);
		});
		
		//修改密码
		$('#changePwd').on('click',function(){
			art.dialog({
				lock: true,
				content:$('#updatePwdDiv').html(),
			    title: '修改密码',
			    width: 400,
			    top:100,
			    ok: function () {
			    	var opwd=$.trim($('#opwd').val());
			    	var pwd=$.trim($('#pwd').val());
			    	var rpwd=$.trim($('#rpwd').val());
			    	if(opwd==''){
			    		alert('原密码不能为空！');
			    		return false;
			    	}
			    	if(pwd==''){
			    		alert('新密码不能为空！');
			    		return false;
			    	}
			    	if(rpwd==''){
			    		alert('重复密码不能为空！');
			    		return false;
			    	}
			    	if(pwd!=rpwd){
			    		alert('两次密码输入不一致！');
			    		return false;
			    	}
			    	var ret=true;
			    	var url = 'common/LoginCtrl/updateUserPwd';
			    	var params = {
		    			'opwd':opwd,
		    			'pwd':pwd
		    		};
		    		var cb = function(data) {
		    			if (data.resultCode != 1) {
		    				alert(data.msg);
		    				ret=false;
		    			}else{
		    				alert('修改成功！');
		    			}
		    		};
		    		sendApiNoLoadSync(url, params, cb, 'POST');
		    		if(!ret)
		    			return false;
			    },
			    cancel: true
			});
		});

        //修改密码
        $('#changeSysUserPassword').on('click',function(){
            layer.open({
                type: 2,
                title: '修改密码',
                skin: 'layui-layer-rim', //加上边框
                area: ['480px', '300px'], //宽高
                content: [APP_ADDRESS +'/mgr/sysMgr/sysUser/sysUserChangePassword.jsp', 'no']
            });
        });

		//退出
		$('#loginOut').on('click',function(){
			var url = 'common/LoginCtrl/loginOut';
			var params = {};
			var cb = function(data) {
				if (data.resultCode != 1) {
					alert(data.msg);
				}else{
					var url=APP_ADDRESS+'login.jsp';
					parent.location.href=url;
				}
			};
			sendApiNoLoad(url, params, cb, 'POST');
		});
	},
	
	getUrl:function(name){
		var url='';
		if(name=="statistics01"){
			//交易统计
			url=APP_ADDRESS+'mgr/statistics/dailyReport/statisticsDailyReport.jsp';
		}else if(name=="userCardDetail"){
			//用户绑卡信息统计
			url=APP_ADDRESS+'mgr/statistics/userCardDetail/userCardDetail.jsp';
		}else if(name=="statistics02"){
			//用户绑卡信息统计
			url=APP_ADDRESS+'mgr/statistics/userInfoReport/userInfoReport.jsp';
		}else if(name=="exceptionOrder"){
			//用户绑卡信息统计
			url=APP_ADDRESS+'mgr/exception/exceptionOrder/exceptionOrder.jsp';
		}else if(name=="userInfoDetail"){
			//用户绑卡信息明细
			url=APP_ADDRESS+'mgr/statistics/userInfoDetail/userInfoDetail.jsp';
		} else if (name == "financialAppDetail") {
			// 理财师使用 app 明细
			url = APP_ADDRESS+'mgr/statistics/financialDetail/financialDetail.jsp';
		} else if(name=="sysUser"){
			url=APP_ADDRESS+'mgr/sysMgr/sysUser.jsp';
		}else if(name=="sysRole"){
			url=APP_ADDRESS+'mgr/sysMgr/sysRole.jsp';
		}else if(name=="sysModule"){
			url=APP_ADDRESS+'mgr/sysMgr/sysModule.jsp';
		}else if(name=="sysResource"){
			url=APP_ADDRESS+'mgr/sysMgr/sysResource.jsp';
		}else if(name=="sysProductVer"){
			//版本控制
			url=APP_ADDRESS+'mgr/sysMgr/appVersion/list.jsp';
		}else if(name=="sysApiSecret"){
			//加密管理
			url=APP_ADDRESS+'mgr/sysMgr/apiSecret/apiSecret.jsp';
		}else if(name=="sysParameter"){
			//参数管理
			url=APP_ADDRESS+'mgr/sysMgr/parameter/sysParameter.jsp';
		}else if(name=="jgPush"){
			//参数管理
			url=APP_ADDRESS+'mgr/sysMgr/jgPush/jgPush.jsp';
		}else if(name=="userChannel"){
			//组织架构
			url=APP_ADDRESS+'mgr/sysMgr/userChannel/userChannel.jsp';
		}else if(name=="organization"){
			//组织架构
			url=APP_ADDRESS+'mgr/sysMgr/organization/organization.jsp';
		}else if(name=="chgLog"){
			//组织架构
			url=APP_ADDRESS+'mgr/sysMgr/chgLog/chgLog.jsp';
		}else if(name=="bespeak"){
			//预约订单
			url=APP_ADDRESS+'mgr/transaction/bespeak/bespeak.jsp';
		}else if(name=="order"){
			//已交易订单
			url=APP_ADDRESS+'mgr/transaction/order/order.jsp';
		}else if(name=="trade"){
			//已交易订单
			url=APP_ADDRESS+'mgr/transaction/trade/trade.jsp';
		}else if(name=="refund"){
			//退款订单
			url=APP_ADDRESS+'mgr/trade/refund/trade.jsp';
		}else if(name=="orderRecheck"){
			//异常订单处理
			url=APP_ADDRESS+'mgr/trade/orderRecheck/orderAbnormal.jsp';
		}else if(name=="department"){
			url=APP_ADDRESS+'mgr/department/department.jsp';
		}else if(name=="duties"){
			url=APP_ADDRESS+'mgr/department/duties.jsp';
		}else if(name=="employee"){
			url=APP_ADDRESS+'mgr/department/employee.jsp';
		}else if(name=="attendance"){
			url=APP_ADDRESS+'mgr/department/attendance.jsp';
		}else if(name=="building"){
			url=APP_ADDRESS+'mgr/product/building.jsp';
		}else if(name=="area"){
			url=APP_ADDRESS+'mgr/product/area.jsp';
		}else if(name=="banBasic"){
			url=APP_ADDRESS+'mgr/product/banBasic.jsp';
		}else if(name=="houseType"){
			url=APP_ADDRESS+'mgr/product/houseType.jsp';
		}else if(name=="banProduct"){
			url=APP_ADDRESS+'mgr/product/banProduct.jsp';
		}else if(name=="payWay"){
			url=APP_ADDRESS+'mgr/product/payWay.jsp';
		}else if(name=="payParameter"){
			url=APP_ADDRESS+'mgr/product/payParameter.jsp';
		}else if(name=="banProductSell"){
			url=APP_ADDRESS+'mgr/product/banProductSell.jsp';
		}else if(name=="customer"){
			url=APP_ADDRESS+'mgr/customer/customer.jsp';
		}else if(name=="productType01"){
			//公墓基金类
			url=APP_ADDRESS+'mgr/product/productType01/productInfo.jsp';
		}else if(name=="productType02"){
			//固定收益类
			url=APP_ADDRESS+'mgr/product/productType02/productInfo.jsp';
		}else if(name=="productType03"){
			//浮动收益类
			url=APP_ADDRESS+'mgr/product/productType03/productInfo.jsp';
		}else if(name=="productType04"){
			//固定+浮动类
			url=APP_ADDRESS+'mgr/product/productType04/productInfo.jsp';
		}else if(name=="productType05"){
			//海外保险类
			url=APP_ADDRESS+'mgr/product/productType05/productInfo.jsp';
		}else if(name=="company"){
			//合作公司
			url=APP_ADDRESS+'mgr/cooperation/company/company.jsp';
		}else if(name=="agreement"){
			//注册协议
			url=APP_ADDRESS+'mgr/other/agreement/agreement.jsp';
		}else if(name=="kyc"){
			//Kyc
			url=APP_ADDRESS+'mgr/other/kyc/question.jsp';
		}else if(name=="suggestion"){
			//意见建议
			url=APP_ADDRESS+'mgr/other/suggestion/suggestion.jsp';
		}else if(name=="branchCompany"){
			//分公司
			url=APP_ADDRESS+'mgr/department/branchCompany/branchCompany.jsp';
		}else if(name=="statistics"){
			//交易统计
			url=APP_ADDRESS+'mgr/statistics/dailyReport/statisticsDailyReport.jsp';
		}else if(name=="activityPlan"){
			//活动策划
			url=APP_ADDRESS+'mgr/activity/activityPlan/activity.jsp';
		}else if(name=="cards"){
			//卡券管理
			url=APP_ADDRESS+'mgr/activity/cards/cards.jsp';
		}else if(name=="cardsHistory"){
			//卡券流水
		url=APP_ADDRESS+'mgr/activity/cardsHistory/cardsHistory.jsp';
		}else if(name=="points"){
			//积分管理
			url=APP_ADDRESS+'mgr/activity/points/points.jsp';
		}else if(name=="pointsHistory"){
			//积分流水
		url=APP_ADDRESS+'mgr/activity/pointsHistory/pointsHistory.jsp';
		}else if(name=="commiStatistics"){
			//佣金统计
			url=APP_ADDRESS+'mgr/commission/commiStatistics/commiStatistics.jsp';
		}else if(name=="commiOrders"){
			//佣金订单
			url=APP_ADDRESS+'mgr/commission/commiOrders/commiOrders.jsp';
		}else if(name=="goldAccount"){
			//安全设置：金账户安全设置
			url=APP_ADDRESS+'mgr/safety/goldAccount/goldAccount.jsp';
		}else if(name == "doubleValidation"){
			//安全设置:二重验证设置
            url=APP_ADDRESS+'mgr/safety/doubleValidation/doubleValidation.jsp';

		}
		
		if(url!=""){
			setLiDisabled();
		}
		return url;
	},
	
	setLoginDate:function(){
		var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	            + " " + date.getHours() + seperator2 + date.getMinutes()
	            + seperator2 + date.getSeconds();
	    $('#loginDate').html(currentdate);
	},
		
	init : function() {
		console.log("in mgr init");
		mgr.startEvent();
		mgr.bindEvent();
	}
};

$(document).ready(function() {
	mgr.init();
	tabUtil.init();
});


function showUserInfo(){
	var url = 'common/LoginCtrl/getCurrentUserInfo';
	var params ={};
	var cb = function(data){
		if(data.resultCode==200){
			var ret=data.result;
			for(var i in ret){
				var text=ret[i];
				if(i=='entryTime'||i=='registCts'){
					text=text==null?'':(new Date(text)).format('yyyy-MM-dd');
				}
				$('#showLoginUser td[name="'+i+'"]').html(text);
			}
		}
	};
	sendApiNoLoadSync(url, params, cb, 'POST');
	art.dialog({
		content:$('#lookUserInfoDiv').html(),
	    title: '个人信息',
	    width: 500,
	    top:100,
	    cancel: true
	});
}

//菜单标签页工具
var tabUtil={
		maxLi:8,
		
		bindEvent:function(){
			$('#disableBlock').on('click',function(e){
				e.stopPropagation();
			});
			$('#tabUl li').unbind();
			$('#tabUl li').on('click',function(){
				$('#tabUl li').removeClass('sel');
				$(this).addClass('sel');
				
				var name=$(this).attr('name');
				$('#tabIframe').find('iframe').each(function(idx,el){
					$(el).css('display','none');
				});
				$('#tabIframe').find('iframe[name="'+name+'"]').css('display','block');
			});
			
			$('#tabUl li i').unbind();
			$('#tabUl li i').on('click',function(){
				var thisLi=$(this).parent();
				if(!thisLi.hasClass('sel')){
					//不是打开状态，直接删除
					$(thisLi).remove();
					$('#tabIframe').find('iframe[name="'+name+'"]').remove();
				}else{
					var name=$(thisLi).attr('name');
					var nextName=''; //关闭后新打开的tab
					$('#tabUl li').each(function(idx,el){
						if(idx==0&&$(el).attr('name')==name){
							//第一个，则是下个打开
							nextName=$(thisLi).next().attr('name');
							return false;
						}else {
							//其他，默认打开前一个
							nextName=$(thisLi).prev().attr('name');
						}
					});
					//删除当前
					$(thisLi).remove();
					$('#tabIframe').find('iframe[name="'+name+'"]').remove();
					//打开新的
					$('#tabUl li[name="'+nextName+'"]').addClass('sel');
					$('#tabIframe').find('iframe[name="'+nextName+'"]').css('display','block');
				}
			});
		},
		
		//根据左边菜单点击，生成新的tab与iframe
		createTab:function(name,txt){
			//先判断是否已经存在
			var existIfa=$('#tabIframe').find('iframe[name="'+name+'"]').attr('name');
			if (typeof(existIfa) != "undefined"){ 
				$('#tabUl li').removeClass('sel');
				$('#tabUl li[name="'+name+'"]').addClass('sel');
				
				$('#tabIframe').find('iframe').each(function(idx,el){
					if($(el).attr('name')!=name){
						$(el).css('display','none');
					}else{
						$(el).css('display','block');
					}
				});
			}else{
				//不存在
				var curLen=$('#tabUl li').length;
				if(curLen==tabUtil.maxLi){
					var yname=$('#tabUl li:last').attr('name');  //原来标签名称
					//更换tab
					$('#tabUl li:last').attr('name',name);
					$('#tabUl li:last').find('span').html(txt);
					//更换iframe
					$('#tabIframe').find('iframe[name="'+yname+'"]').attr('name',name);
				}else{
					$('#tabUl li').removeClass('sel');
					var newLi='<li class="sel" name="'+name+'"><span>'+txt+'</span><i class="fa fa-close"></i>  </li>';
					$('#tabUl').append(newLi);
					
					$('#tabIframe').find('iframe').each(function(idx,el){
						$(el).css('display','none');
					});
					var winHeight=$(window).height();
					var ifrHeight=0;
					if(mgr.hasAll){
						ifrHeight=winHeight-34;
					}else{
						ifrHeight=winHeight-88;
					}

					var newIframe='<iframe name="'+name+'" class="rightIfms" id = "'+name+'" onload=changeFrameHeight("' + name +'")   width="100%" height="'+ifrHeight+'" frameborder="0" src=""></iframe>';
					$('#tabIframe').append(newIframe);
					tabUtil.bindEvent();
				}
			}


			////改变大小
			//var iframes = $('.rightIfms');
			//console.log("will update chgInframesize");
			//console.log(iframes);
			//for( var iframe in iframes){
			//	$(iframe).contents().find("body").resize(function(){
			//		//你需要的功能
			//		alert("窗口大小改变");
			//	});
			//}

		},
		
		init:function(){
			tabUtil.bindEvent();
		}
}

function setLiDisabled() {
	$("#disableBlock").css('display',' block');
	//$("li").attr('disabled','disabled');
	//var a=document.getElementById('sysMgr');
	//a=document.getElementById('sysMgr');
	//alert($("li").attr('disabled'));
}

function setLiAbled () {
	$("#disableBlock").css('display',' none');
	//$("li").attr('disabled',false); 
	//alert($("li").attr('disabled'));
}



function changeFrameHeight(name){
	//var ifm= document.getElementById(name);
	//ifm.height= 1200;
	//var subWeb = document.frames ? document.frames[name].document : ifm.contentDocument;
	//if(ifm != null && subWeb != null) {
	//	console.log("subWeb.body.scrollHeight = " + subWeb.body.scrollHeight);
	//	ifm.height = subWeb.body.scrollHeight;
	//}


	//console.log(name);
	//console.log("id = " + name)
	//console.log("will change hight name = " + name);
	//var ifm= document.getElementById(name);
    //
	//console.log($(ifm).contents().find(".box"));
	//console.log("body hight = " + $(ifm).contents().find(".box").height());
    //
    //
	//var box = ifm.getElementsByClassName("box");
	//console.log(box);
	//console.log("height01 = " + ifm.clientHeight); //  name.document.documentElement.clientHeight);
    //
	//console.log("height02 = " + ifm.scrollHeight);
    //
	//console.log("height = " +  height);
	////if(height < 925){
	////	height = 925;
	////}
    //
	//ifm.height= ifm.body.scrollHeight;

}

window.onresize=function(){
	changeFrameHeight();
}


